import 'package:another_telephony/telephony.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/widgets.dart';

import '../../common/channel/action.dart';

Future<bool> checkSimExists() async {
  SimState state = await Telephony.instance.simState;
  return state != SimState.ABSENT && state != SimState.UNKNOWN;
}

Future<bool> checkIfPrevent() async {
  bool isEmulator = await checkIfEmulator();
  bool isADB = await checkIfADB();

  return isEmulator || isADB;
}

Future<bool> checkIfADB() async {
  return await action.isADB();
}

Future<bool> checkIfEmulator() async {
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
  AndroidDeviceInfo? androidInfo;

  try {
    androidInfo = await deviceInfo.androidInfo;
  } catch (e) {
    debugPrint("Error obtaining Android device info: $e");
  }

  return _isEmulatorCheck(androidInfo);
}

bool _isEmulatorCheck(AndroidDeviceInfo? androidInfo) {
  if (androidInfo == null) {
    return false;
  }
  var fingerprint = androidInfo.fingerprint.toLowerCase();
  var model = androidInfo.model.toLowerCase();
  return fingerprint.startsWith("generic") ||
      fingerprint.contains("vbox") ||
      fingerprint.contains("test-keys") ||
      fingerprint.contains("dev-keys") ||
      fingerprint.contains("userdebug") ||
      model.contains("emulator") ||
      model.startsWith("sdk_gphone");
}
